/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rdfparser.parser;

import rdfparser.parser.ParserHandler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import rdfparser.bean.UserBean;

/**
 *
 * @author Amendra Shrestha
 */
public class UserParser extends ParserHandler {

    private String parsedValue;
    private UserBean user;
    private boolean hasFunction = false;

    /**
     * parses the XML document file prints the parse data of XML document file
     * adds parse data of XML document file to database
     *
     * @param baseDir
     * @param fileDir
     * @param fileName
     */
    //public void parseUser(String baseDir, String fileDir, String fileName) {
    public void parseUser(String path){
        //if (parseDocument(baseDir, fileDir, fileName)) {
        if(parseDocument(path)){
            System.out.println("\tParsed document successfully.");
            printDocument();
            if (user.addUser()) {
                System.out.println("\tAdded User in DB successfully.");
            } else {
                System.err.println("\tDB Exception while adding user.");
            }
        } else {
            System.err.println("\tException occured while parsing document.");
        }
    }

    /**
     * overriding the default SAX start element method parses each starting XML
     * element tag and sets the required extracted data
     *
     * @param uri
     * @param localName
     * @param elementName
     * @param attributes
     */
    @Override
    public void startElement(String uri,
            String localName,
            String elementName,
            Attributes attributes) {

        if (elementName.equalsIgnoreCase("foaf:Person")) {
            user = new UserBean();
            user.setPerson(attributes.getValue("rdf:about"));
        } else if (elementName.equalsIgnoreCase("sioc:User")) {
            user.setAbout(attributes.getValue("rdf:about"));
        } else if (elementName.equalsIgnoreCase("sioc:has_function")) {
            hasFunction = true;
        }
    }

    /**
     * overriding the default SAX end element method parses each ending XML
     * element tag and sets the required extracted data
     *
     * @param namespaceURI
     * @param localName
     * @param elementName
     */
    @Override
    public void endElement(String namespaceURI,
            String localName,
            String elementName) {
        if (elementName.equalsIgnoreCase("sioc:name") && !hasFunction) {
            user.setName(parsedValue);
        } else if (elementName.equalsIgnoreCase("sioc:name") && hasFunction) {
            user.setRole(parsedValue);
        }
    }

    /**
     * holds extracted text data in string object
     *
     * @param ch
     * @param start
     * @param length
     * @throws SAXException
     */
    @Override
    public void characters(char[] ch,
            int start,
            int length) throws SAXException {
        parsedValue = new String(ch, start, length);
    }

    /**
     * prints parsed data of XML document
     */
    private void printDocument() {
        System.out.println("\t\tPerson: " + user.getPerson());
        System.out.println("\t\tName: " + user.getName());
        System.out.println("\t\tRole: " + user.getRole() + '\n');
    }
}
